//
//  dadaTouchView.m
//  qqqqqq
//
//  Created by iOSer on 2016/12/21.
//  Copyright © 2016年 iOSer. All rights reserved.
//
#define orangeColor HEXColor(0xff9c00, 1)
#define gryColor HEXColor(0xd7d7d7,1)
#import "dadaTouchView.h"
@interface dadaTouchView()<CAAnimationDelegate>
@property(nonatomic,strong) UILabel *hintLab;
@property(nonatomic,strong) UIView *animationView;
@property(nonatomic,strong) CAShapeLayer *bgLayer;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property(nonatomic,strong) CAShapeLayer *progressLayer;
@property (strong,nonatomic) CALayer *blockHeaderBgLayer;
@property (strong,nonatomic) CALayer *blockHeaderlayer;
@property (strong,nonatomic) CALayer *bigPointLayer;
@property (strong,nonatomic) CALayer *smallPointLayer;
@property(nonatomic,strong) UIBezierPath *bgPath;
@property(nonatomic,strong) UIBezierPath *shapePath;
@property (strong,nonatomic) CALayer *blockBody;
@property (strong,nonatomic) CABasicAnimation *progressAnimation;
@end
@implementation dadaTouchView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self setupUI];
        [self setupLayer];
    }
    return self;
}
-(void)setupUI{
    [self addSubview:self.animationView];
    [self addSubview:self.hintLab];
    [_animationView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    [_hintLab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(GGRealValue(110));
    }];
}

-(void)setupLayer{
    
    //背景图层
    self.bgLayer.frame = self.frame;
    self.bgLayer.position = self.center;
//    [self.bgLayer setBackgroundColor:(__bridge CGColorRef _Nullable)(HEXColor(0xBDBDBD, 1))];
   
    self.bgLayer.path = self.bgPath.CGPath;
    [self.animationView.layer addSublayer:self.bgLayer];
    
    
    //橘色圆环
    CGFloat shapeWidth = self.bounds.size.width-60;
    CGFloat shapeHeight = self.bounds.size.height-60;
    self.shapeLayer.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, shapeWidth, shapeHeight);
    self.shapeLayer.position = CGPointMake(self.bgLayer.position.x-30, self.bgLayer.position.y-30);
    self.shapeLayer.path = self.shapePath.CGPath;
    [self.bgLayer addSublayer:self.shapeLayer];
    
    //锁身
    [self.shapeLayer addSublayer:self.blockBody];
    //锁眼
    self.bigPointLayer.position = CGPointMake(self.blockBody.position.x, self.blockBody.position.y-((self.bigPointLayer.frame.size.width/2)-0.5));
    self.smallPointLayer.position = CGPointMake(self.blockBody.position.x, self.blockBody.position.y+((self.smallPointLayer.frame.size.width/2)-0.5));
    [self.shapeLayer addSublayer:self.bigPointLayer];
    [self.shapeLayer addSublayer:self.smallPointLayer];
    
    //锁环
//    self.blockHeaderlayer.frame = CGRectMake(0, 0, GGRealValue(24), GGRealValue(34));
//    self.blockHeaderlayer.position = CGPointMake(self.center.x,( _blockBody.position.y-(_blockBody.frame.size.height/2)));
    self.blockHeaderBgLayer.position = CGPointMake(self.center.x,( _blockBody.position.y-(_blockBody.frame.size.height/2)-(GGRealValue(17)/2)));
    [self.blockHeaderBgLayer addSublayer:self.blockHeaderlayer];
    [self.shapeLayer addSublayer:self.blockHeaderBgLayer];

    
    //外进度环
    self.progressLayer.frame = self.frame;
    self.progressLayer.position = self.center;
    self.progressLayer.path = self.bgPath.CGPath;
    [self.bgLayer addSublayer:self.progressLayer];
    [self beastAnimagtionMake];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    self.shapeLayer.strokeEnd = 1;
    _hintLab.textColor = HEXColor(0xffffff, 1);
    _bgLayer.fillColor = [HEXColor(0xd7d7d7, 0) CGColor];
    [self blockTouchBeginMake];
    [self.bgLayer removeAnimationForKey:@"pathAnimation"];
    [self.bgLayer removeAnimationForKey:@"colorAnimation"];
    [self progressAnimagtionMake];
    [self blockAnimationMake];
    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //移除动画
    [self.progressLayer removeAnimationForKey:@"progressAnimation"];
    [self.blockHeaderBgLayer removeAnimationForKey:@"blockHeaderAnimation"];
    [self.blockBody removeAnimationForKey:@"blockBodyAnimation"];
    [self.shapeLayer removeAnimationForKey:@"shapeAnimation"];
    //参数还原
    _shapeLayer.fillColor = HEXColor(0xffffff, 1).CGColor;
    _hintLab.textColor = orangeColor;
    CGFloat headerY = ( _blockBody.position.y-(_blockBody.frame.size.height/2)-(GGRealValue(17)/2));
    CGFloat headerX = self.blockHeaderBgLayer.position.x;
    self.blockHeaderBgLayer.position = CGPointMake(headerX, headerY);
    [self beastAnimagtionMake];
    [self blockTouchEndMake];
    self.hintLab.transform = CGAffineTransformIdentity;
    
}
#pragma mark - 动画
//默认动画 灰色背景圆放大，颜色变化
-(void)beastAnimagtionMake{
    
    CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    progressAnimation.duration = 2;
    progressAnimation.repeatCount = HUGE_VALF;
    progressAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    progressAnimation.fromValue = (__bridge id _Nullable)(_shapePath.CGPath);
    progressAnimation.toValue = (__bridge id _Nullable)(_bgPath.CGPath);
    progressAnimation.fillMode = kCAFillModeForwards;
    progressAnimation.removedOnCompletion = NO;
    [self.bgLayer addAnimation:progressAnimation forKey:@"pathAnimation"];
    
    CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"];
    colorAnimation.duration = 2;
    colorAnimation.repeatCount = HUGE_VALF;
    colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    colorAnimation.fromValue = (__bridge id _Nullable)([gryColor CGColor]);
    colorAnimation.toValue = (__bridge id _Nullable)([HEXColor(0xd7d7d7, 0) CGColor]);
    colorAnimation.fillMode = kCAFillModeForwards;
    colorAnimation.removedOnCompletion = NO;
    [self.bgLayer addAnimation:colorAnimation forKey:@"colorAnimation"];
}
//按下后进度条动画
-(void)progressAnimagtionMake{
    _shapeLayer.fillColor = orangeColor.CGColor;
    [self.progressLayer addAnimation:self.progressAnimation forKey:@"progressAnimation"];
  
}
//锁变化动画
-(void)blockAnimationMake{
    //锁头
    {
    CGFloat headerY = ( _blockBody.position.y-(_blockBody.frame.size.height/2)-(GGRealValue(17)/2));
    CGFloat headerX = self.blockHeaderBgLayer.position.x;
    self.blockHeaderBgLayer.position = CGPointMake(headerX,headerY-(self.blockHeaderBgLayer.frame.size.height/4));
    CGPoint headerBgPosition = CGPointMake(headerX,headerY-(self.blockHeaderBgLayer.frame.size.height/4));
    CGPoint headerBgChangePosition = CGPointMake(headerX,headerY-(self.blockHeaderBgLayer.frame.size.height/1.5));
    CABasicAnimation *blockHeaderAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    blockHeaderAnimation.duration = 1.8;
    blockHeaderAnimation.repeatCount = HUGE_VALF;
    blockHeaderAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    blockHeaderAnimation.fromValue = [NSValue valueWithCGPoint: headerBgPosition];
    blockHeaderAnimation.toValue = [NSValue valueWithCGPoint: headerBgChangePosition];
    blockHeaderAnimation.fillMode = kCAFillModeForwards;
    blockHeaderAnimation.removedOnCompletion = NO;
    blockHeaderAnimation.autoreverses = YES;
    [self.blockHeaderBgLayer addAnimation:blockHeaderAnimation forKey:@"blockHeaderAnimation"];
    }
    //锁身
    {
        CABasicAnimation *blockBodyAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        blockBodyAnimation.duration = 2;
        blockBodyAnimation.repeatCount = HUGE_VALF;
        blockBodyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//        blockBodyAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, bodySize.width, bodySize.height)];
//        blockBodyAnimation.toValue = [NSValue valueWithCGRect: CGRectMake(0, 0, bodyChangeSize.width, bodyChangeSize.height)];
        blockBodyAnimation.fromValue = [NSNumber numberWithFloat:1];
        blockBodyAnimation.toValue = [NSNumber numberWithFloat:1.2];
        blockBodyAnimation.fillMode = kCAFillModeForwards;
        blockBodyAnimation.removedOnCompletion = NO;
        blockBodyAnimation.autoreverses = YES;
        [self.blockBody addAnimation:blockBodyAnimation forKey:@"blockBodyAnimation"];
    }
    //文字
    {
        CGAffineTransform transform = CGAffineTransformScale(self.hintLab.transform, 1.1, 1.1);
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [self.hintLab setTransform:transform];
        [UIView commitAnimations];
    }
    //内圈放大
    {
    UIBezierPath *shapeChangePath = [UIBezierPath bezierPathWithArcCenter:self.bgLayer.position radius:(self.shapeLayer.frame.size.width+10)/2 startAngle:- M_PI_2 endAngle:M_PI+M_PI_2 clockwise:YES];
    CABasicAnimation *shapeAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    shapeAnimation.duration = 1.8;
    shapeAnimation.repeatCount = HUGE_VALF;
    shapeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    shapeAnimation.fromValue = (__bridge id _Nullable)(_shapePath.CGPath);
    shapeAnimation.toValue = (__bridge id _Nullable)(shapeChangePath.CGPath);
    shapeAnimation.fillMode = kCAFillModeForwards;
    shapeAnimation.removedOnCompletion = NO;
    shapeAnimation.autoreverses = YES;
    [self.shapeLayer addAnimation:shapeAnimation forKey:@"shapeAnimation"];
    }
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if (anim == [self.progressLayer animationForKey:@"progressAnimation"]) {
        if (flag == YES) {
            [self.deledate progressAnimationEnd];
        }
    }
    
}
#pragma mark - UI数据变化
//锁变化
-(void)blockTouchBeginMake{
    [_blockBody setBackgroundColor:[orangeColor CGColor]];
    [_blockBody setBorderColor:[[UIColor whiteColor] CGColor]];
    [_blockHeaderlayer setBackgroundColor:[orangeColor CGColor]];
    [_blockHeaderlayer setBorderColor:[[UIColor whiteColor] CGColor]];
    [_bigPointLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [_bigPointLayer setBorderColor:[[UIColor whiteColor] CGColor]];
    [_smallPointLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [_smallPointLayer setBorderColor:[[UIColor whiteColor] CGColor]];
    [_blockHeaderBgLayer setBackgroundColor:[orangeColor CGColor]];
}

-(void)blockTouchEndMake{
    [_blockBody setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [_blockBody setBorderColor:[orangeColor CGColor]];
    [_blockHeaderlayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [_blockHeaderlayer setBorderColor:[orangeColor CGColor]];
    [_bigPointLayer setBackgroundColor:[orangeColor CGColor]];
    [_bigPointLayer setBorderColor:[orangeColor CGColor]];
    [_smallPointLayer setBackgroundColor:[orangeColor CGColor]];
    [_smallPointLayer setBorderColor:[orangeColor CGColor]];
    [_blockHeaderBgLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
}



#pragma mark - 懒加载
-(CAShapeLayer *)shapeLayer{
    if (!_shapeLayer) {
        _shapeLayer = [[CAShapeLayer alloc] init];
        _shapeLayer.fillColor = [[UIColor whiteColor] CGColor];
        _shapeLayer.strokeColor = orangeColor.CGColor;
        _shapeLayer.lineWidth = 3;
    }
    return _shapeLayer;
}
-(CAShapeLayer *)progressLayer{
    if (!_progressLayer) {
        _progressLayer = [[CAShapeLayer alloc] init];
        _progressLayer.fillColor = [[UIColor clearColor] CGColor];
        _progressLayer.strokeColor = orangeColor.CGColor;
        _progressLayer.strokeStart = 0;
        _progressLayer.strokeEnd = 0;
        //        _shapeLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的
        //        _shapeLayer.lineJoin = kCALineCapRound;
        _progressLayer.lineWidth = 4;
    }
    return _progressLayer;
}
-(CAShapeLayer *)bgLayer{
    if (!_bgLayer) {
        _bgLayer = [[CAShapeLayer alloc] init];
        _bgLayer.fillColor = [gryColor CGColor];
        _bgLayer.strokeColor = [UIColor clearColor].CGColor;
    }
    return _bgLayer;
}
-(UIBezierPath *)bgPath{
    if (!_bgPath) {
         _bgPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:self.frame.size.width/2 startAngle:- M_PI_2 endAngle:M_PI+M_PI_2 clockwise:YES];
    }
    return _bgPath;
}
-(UIBezierPath *)shapePath{
    if (!_shapePath) {
        _shapePath = [UIBezierPath bezierPathWithArcCenter:self.bgLayer.position radius:self.shapeLayer.frame.size.width/2 startAngle:- M_PI_2 endAngle:M_PI+M_PI_2 clockwise:YES];
    }
    return _shapePath;
}
-(UILabel *)hintLab{
    if (!_hintLab) {
        _hintLab = [[UILabel alloc] init];
        _hintLab.text = @"长按验票";
        _hintLab.textColor = orangeColor;
        _hintLab.font = [UIFont systemFontOfSize:GGRealValue(13)];
    }
    return _hintLab;
}
-(UIView *)animationView{
    if (!_animationView) {
        _animationView = [UIView new];
        [_animationView setBackgroundColor:HEXColor(0xffffff, 1)];
    }
    return _animationView;
}
-(CALayer *)blockBody{
    if (!_blockBody) {
        _blockBody = [[CALayer alloc] init];
        _blockBody.frame = CGRectMake(0.0f, 0.0f, GGRealValue(37), GGRealValue(30));
//        [_blockBody setBounds:shapeRect];
        [_blockBody setPosition:self.center];
        [_blockBody setBackgroundColor:[[UIColor whiteColor] CGColor]];
        [_blockBody setBorderColor:[orangeColor CGColor]];
        [_blockBody setBorderWidth:3.0f];
        _blockBody.masksToBounds = YES;
        [_blockBody setOpacity:1];
        [_blockBody setCornerRadius:6.0];
    }
    return _blockBody;
}
-(CALayer *)blockHeaderlayer{
    if (!_blockHeaderlayer) {
        _blockHeaderlayer = [[CALayer alloc] init];
        _blockHeaderlayer.frame = CGRectMake(0, 0, GGRealValue(24), GGRealValue(34));
        //        [_blockBody setBounds:shapeRect];
        [_blockHeaderlayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
        [_blockHeaderlayer setBorderColor:[orangeColor CGColor]];
        [_blockHeaderlayer setBorderWidth:3.0f];
        [_blockHeaderlayer setOpacity:1];
        [_blockHeaderlayer setCornerRadius:GGRealValue(12)];
    }
    return _blockHeaderlayer;
}
-(CALayer *)bigPointLayer{
    if (!_bigPointLayer) {
        _bigPointLayer = [[CALayer alloc] init];
        _bigPointLayer.frame = CGRectMake(0, 0, GGRealValue(6), GGRealValue(6));
        //        [_blockBody setBounds:shapeRect];
        [_bigPointLayer setBackgroundColor:[orangeColor CGColor]];
        [_bigPointLayer setBorderColor:[orangeColor CGColor]];
        [_bigPointLayer setBorderWidth:1.0f];
        [_bigPointLayer setOpacity:1];
        [_bigPointLayer setCornerRadius:GGRealValue(3)];
    }
    return _bigPointLayer;
}
-(CALayer *)smallPointLayer{
    if (!_smallPointLayer) {
        _smallPointLayer = [[CALayer alloc] init];
        _smallPointLayer.frame = CGRectMake(0, 0, GGRealValue(4.5), GGRealValue(4.5));
        //        [_blockBody setBounds:shapeRect];
        [_smallPointLayer setBackgroundColor:[orangeColor CGColor]];
        [_smallPointLayer setBorderColor:[orangeColor CGColor]];
        [_smallPointLayer setBorderWidth:1.0f];
        [_smallPointLayer setOpacity:1];
        [_smallPointLayer setCornerRadius:GGRealValue(4.5/2)];
    }
    return _smallPointLayer;
}
-(CALayer *)blockHeaderBgLayer{
    if (!_blockHeaderBgLayer) {
        _blockHeaderBgLayer = [[CALayer alloc] init];
        
        _blockHeaderBgLayer.frame = CGRectMake(0, 0, GGRealValue(24), GGRealValue(17));
        [_blockHeaderBgLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
        _blockHeaderBgLayer.masksToBounds = YES;
    }
    return _blockHeaderBgLayer;
}

-(CABasicAnimation *)progressAnimation{
    if (!_progressAnimation) {
        _progressAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        _progressAnimation.duration = 1.5;
        _progressAnimation.repeatCount = 1;
        _progressAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        _progressAnimation.fromValue = @0;
        _progressAnimation.toValue = @1;
        _progressAnimation.fillMode = kCAFillModeForwards;
        _progressAnimation.removedOnCompletion = NO;
        _progressAnimation.delegate = self;
    }
    return _progressAnimation;
}
@end
